package org.szgb.console.sys.dict.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;
import org.szgb.console.utils.PageDto;
import org.szgb.core.base.entity.BeanEntity;

import java.io.Serial;
import java.time.LocalDateTime;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class SysDict extends BeanEntity<SysDict> {
    @Serial
    private static final long serialVersionUID = 1L;
    private String name;

    public SysDict() {
    }

    public SysDict(String id) {
        this.setId(id);
    }

    public SysDict(SysDict.SysDictFindDto sysDictFindDto) {
        this.setId(sysDictFindDto.getId());
        this.setName(sysDictFindDto.getName());
        this.setPageDto(sysDictFindDto.getPageDto());
    }

    public SysDict(SysDict.SysDictSaveDto sysDictSaveDto) {
        this.setId(sysDictSaveDto.id);
        this.setName(sysDictSaveDto.name);
    }

    public SysDict.SysDictView toSysDictView() {
        return new SysDict.SysDictView(this);
    }

    public SysDict.SysDictTreeView toSysDictTreeView(List<SysDictChildren.SysDictChildrenTreeView> childrenTreeViewList) {
        return new SysDict.SysDictTreeView(this, childrenTreeViewList);
    }

    @Data
    @Schema(title = "字典信息", description = "字典信息")
    public static class SysDictView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "名称")
        private String name;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "创建时间")
        private LocalDateTime createdAt;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "更新时间")
        private LocalDateTime updatedAt;
        @Schema(description = "创建人id")
        private String createdById;
        @Schema(description = "创建人姓名")
        private String createdByName;
        @Schema(description = "更新人id")
        private String updatedById;
        @Schema(description = "更新人姓名")
        private String updatedByName;

        public SysDictView(SysDict sysDict) {
            this.id = sysDict.getId();
            this.name = sysDict.getName();
            this.createdAt = sysDict.getCreatedAt();
            this.updatedAt = sysDict.getUpdatedAt();
            if (sysDict.getCreatedBy() != null) {
                this.createdById = sysDict.getCreatedBy().getId();
                this.createdByName = sysDict.getCreatedBy().getName();
            }
            if (sysDict.getUpdatedBy() != null) {
                this.updatedById = sysDict.getUpdatedBy().getId();
                this.updatedByName = sysDict.getUpdatedBy().getName();
            }
        }

        public static List<SysDict.SysDictView> getViewListFromList(List<SysDict> sysDictList) {
            return sysDictList.stream().map(SysDict.SysDictView::new).toList();
        }
    }

    @Data
    @Schema(title = "字典树信息", description = "字典树信息")
    public static class SysDictTreeView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "名称")
        private String name;
        @Schema(description = "子项")
        private List<SysDictChildren.SysDictChildrenTreeView> children;

        public SysDictTreeView(SysDict sysDict, List<SysDictChildren.SysDictChildrenTreeView> children) {
            this.id = sysDict.getId();
            this.name = sysDict.getName();
            this.children = children;
        }
    }

    @Data
    @Schema(title = "字典查询表单", description = "字典查询参数")
    public static class SysDictFindDto {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "名称")
        private String name;
        @Schema(description = "分页信息")
        private PageDto pageDto;

        public SysDict toSysDict() {
            return new SysDict(this);
        }
    }

    @Data
    @Schema(title = "字典保存表单", description = "字典保存参数")
    public static class SysDictSaveDto {
        @Schema(description = "id(如为空则为新增数据)")
        private String id;
        @Schema(description = "名称")
        private String name;

        public SysDict toSysDict() {
            return new SysDict(this);
        }
    }
}
